package com.lc.rongzecaifu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.rongzecaifu.MyApplication;
import com.lc.rongzecaifu.conn.JsonAccessToken;
import com.lc.rongzecaifu.conn.JsonChangemobile;
import com.lc.rongzecaifu.conn.JsonUserinfo;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.lc.rongzecaifu.toast.LovelyToast;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;
import com.zcx.helper.util.UtilToast;

import cn.trinea.android.common.util.ToastUtils;

public class SafetyCertificateActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivback;
    private TextView tv01;
    private ImageView ivam01;
    private TextView tv02;
    private ImageView ivam02;
    private ImageView iv01;
    private ImageView iv02;


    private LinearLayout real_name_layout;

    private LinearLayout bind_mobil_layout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safety_certificate);

        initView();

    }

    private void initView() {
        this.ivam02 = (ImageView) findViewById(R.id.iv_am_02);
        this.tv02 = (TextView) findViewById(R.id.tv_02);
        this.iv02 = (ImageView) findViewById(R.id.iv_02);
        this.ivam01 = (ImageView) findViewById(R.id.iv_am_01);
        this.tv01 = (TextView) findViewById(R.id.tv_01);
        this.iv01 = (ImageView) findViewById(R.id.iv_01);
        this.ivback = (ImageView) findViewById(R.id.iv_back);

        bind_mobil_layout = (LinearLayout) findViewById(R.id.bind_mobil_layout);

        bind_mobil_layout.setOnClickListener(this);
        real_name_layout = (LinearLayout) findViewById(R.id.real_name_layout);
        real_name_layout.setOnClickListener(this);

        real_name_layout.setSelected(true);
        iv02.setSelected(true);
        if (MyApplication.myPreferences.readId_status().equals("0")) {
            iv01.setSelected(false);
            tv01.setText("未认证");
        } else if (MyApplication.myPreferences.readId_status().equals("2")) {
            iv01.setSelected(false);
            tv01.setText("审核中");
        } else if (MyApplication.myPreferences.readId_status().equals("0")) {
            real_name_layout.setSelected(false);
        } else if (MyApplication.myPreferences.readId_status().equals("2")) {
            real_name_layout.setSelected(false);
        } else {
            iv01.setSelected(true);
//            tv01.setText(MyApplication.myPreferences.readReal_name());
            tv01.setText("已认证");
            ivam01.setVisibility(View.VISIBLE);

            real_name_layout.setSelected(true);

        }
        String phone = MyApplication.myPreferences.readUser_phone();
        if (!TextUtils.isEmpty(phone) && UtilMatches.checkMobile(phone)) {
            tv02.setText(phone.substring(0, 3) + "****" + phone.substring(7, 11));
        }


    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.real_name_layout:

                if (MyApplication.myPreferences.readId_status().equals("0")) {
                    startVerifyActivity(VerifiedActivity.class);
                } else if (MyApplication.myPreferences.readId_status().equals("1")) {
                    ToastUtils.show(this, "您已通过实名认证");
                } else if (MyApplication.myPreferences.readId_status().equals("2")) {
                    ToastUtils.show(this, "您的实名认证申请正在审核中");
                }

                break;

            case R.id.bind_mobil_layout:
                getNextAccessToken();
                //  startVerifyActivity(ChangePhoneNumbersActivity.class);

                break;


        }
    }

    @Override
    protected void onResume() {

        super.onResume();
        getAccessToken();
      /*  if (MyApplication.myPreferences.readId_status().equals("0")) {

            iv01.setSelected(false);
            tv01.setText("未认证");

        } else if (MyApplication.myPreferences.readId_status().equals("2")) {

            iv01.setSelected(false);
            tv01.setText("审核中");

        } else {

            iv01.setSelected(true);
            tv01.setText("已认证");
            ivam01.setVisibility(View.VISIBLE);

        }

        String phone = MyApplication.myPreferences.readUser_phone();

        if (!TextUtils.isEmpty(phone) && UtilMatches.checkMobile(phone)) {

            tv02.setText(phone.substring(0, 3) + "****" + phone.substring(7, 11));

        }
*/

    }


    private void getAccessToken() {
        new JsonAccessToken("user/userinfo", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        onGetUserInfo(info.access_token);
                    } else {
                        //UtilToast.show(getActivity(), "安全验证失败！");
                        LovelyToast.makeText(SafetyCertificateActivity.this, "安全验证失败！", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }

                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                // UtilToast.show(getActivity(), "安全验证失败！");
                LovelyToast.makeText(SafetyCertificateActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(SafetyCertificateActivity.this, false);
    }

    private void onGetUserInfo(String access_token) {
        new JsonUserinfo(access_token, MyApplication.myPreferences.readUid(), new AsyCallBack<JsonUserinfo.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonUserinfo.Info info) throws Exception {
                if (info != null) {
                    if (MyApplication.myPreferences.readId_status().equals("0")) {

                        iv01.setSelected(false);
                        tv01.setText("未认证");

                    } else if (MyApplication.myPreferences.readId_status().equals("2")) {

                        iv01.setSelected(false);
                        tv01.setText("审核中");

                    } else {

                        iv01.setSelected(true);
                        tv01.setText("已认证");
                        ivam01.setVisibility(View.VISIBLE);

                    }

                  //  String phone = MyApplication.myPreferences.readUser_phone();
                    String phone = info.user_phone;
                    if (!TextUtils.isEmpty(phone) && UtilMatches.checkMobile(phone)) {

                        tv02.setText(phone.substring(0, 3) + "****" + phone.substring(7, 11));

                    }

                    MyApplication.myPreferences.saveUid(info.id);

                    MyApplication.myPreferences.saveUserName(info.user_name);

                    MyApplication.myPreferences.saveUser_email(info.user_email);

                    MyApplication.myPreferences.saveUser_phone(info.user_phone);

                    MyApplication.myPreferences.saveSex(info.sex);

                    MyApplication.myPreferences.saveReal_name(info.real_name);

                    MyApplication.myPreferences.saveIdcard(info.idcard);

                    MyApplication.myPreferences.saveMoney_freeze(info.money_freeze);

                    MyApplication.myPreferences.saveMoney_collect(info.money_collect);

                    MyApplication.myPreferences.saveAccount_money(info.account_money);

                    MyApplication.myPreferences.saveMoney_experience(info.money_experience);

                    MyApplication.myPreferences.saveAll_money(info.all_money);

                    MyApplication.myPreferences.saveUserpic(info.userpic);

                    MyApplication.myPreferences.saveId_status(info.id_status);

                    MyApplication.myPreferences.saveVip_status(info.vip_status);

                    MyApplication.myPreferences.savePhone_status(info.phone_status);

                    MyApplication.myPreferences.saveEmail_status(info.email_status);

                    MyApplication.myPreferences.savePin_pass(info.pin_pass);
                    MyApplication.myPreferences.saveNickName(info.nick_name);
                    MyApplication.myPreferences.saveIncode(info.incode);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                LovelyToast.makeText(SafetyCertificateActivity.this, JsonUserinfo.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

        }
        ).
                execute(SafetyCertificateActivity.this,
                        false);

    }


    private void getNextAccessToken() {
        new JsonAccessToken("user/change_mobile", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        goNext(info.access_token);
                    } else {
                        UtilToast.show(SafetyCertificateActivity.this, "安全验证失败！");
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                UtilToast.show(SafetyCertificateActivity.this, "安全验证失败！");
            }
        }).execute(this, true);

    }


    private void goNext(String access_token) {
        new JsonChangemobile(access_token, MyApplication.myPreferences.readUid(), new AsyCallBack<JsonChangemobile.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonChangemobile.Info info) throws Exception {
                Intent intent = new Intent();


                intent.putExtra("url", info.url);

                //   intent.setClass(SafetyCertificateActivity.this, ChangePhoneWebActivity.class);
                intent.setClass(SafetyCertificateActivity.this, ChangePhoneWebActivity.class);
                startActivity(intent);

                //  Toast.makeText(context, "请求成功", Toast.LENGTH_SHORT).show();

                //  finish();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }

        }).execute(this, true);

    }


}
